package com.charmzenus.champion.adapter.main;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.charmzenus.champion.R;
import com.charmzenus.champion.adapter.ItemClickListener;

import java.util.List;

/**
 * Created by a1899 on 2018/4/26.
 */

public class MainOrderAdapter extends RecyclerView.Adapter<MainOrderAdapter.MainOrderHolder> {

    private Context context;
    private List<String> mData;
    private ItemClickListener listener;
    private LayoutInflater mInflater;

    public MainOrderAdapter(Context context, List<String> data) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    @Override
    public MainOrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_main_order_layout, parent, false);
        return new MainOrderHolder(view, listener);
    }

    @Override
    public void onBindViewHolder(MainOrderHolder holder, int position) {
        holder.tv.setText(mData.get(position));
    }

    public void setListener(ItemClickListener listener) {
        this.listener = listener;
    }

    public void setListData(List<String> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (mData == null) return 0;
        return mData.size();
    }

    class MainOrderHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView tv;

        public MainOrderHolder(View itemView, final ItemClickListener mListener) {
            super(itemView);
            this.itemView = itemView;
            tv = itemView.findViewById(R.id.item_main_order_tv);

            if (mListener == null) return;

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(v, getAdapterPosition());
                }
            });
        }
    }
}
